﻿using QLTour.Biz;
using QLTour.Ultilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLTour.Dal
{
    public class DiaDiemDal
    {
        DatabaseHelper db = new DatabaseHelper();
        public DiaDiemDal()
        {

        }
        /* public List<DiaDiemBiz> GetList()
        {
            List<DiaDiemBiz> list = new List<DiaDiemBiz>();
            SqlDataReader reader = db.Reader("SELECT * FROM [DiaDiem]");
            while (reader.Read())
            {
                DiaDiemBiz dd = new DiaDiemBiz();
                dd.MaDiaDiem = reader["MaDiaDiem"].ToString();
                dd.Ten = reader["Ten"].ToString();
                list.Add(dd);
            }
            return list;
        } */
        /* public List<ThamQuanBiz> GetListByTour(string matour)
        {
            List<ThamQuanBiz> list = new List<ThamQuanBiz>();
            SqlDataReader reader = db.Reader("SELECT * FROM [ThamQuan] WHERE [MaTour]='"+matour+"'");
            while (reader.Read())
            {
                ThamQuanBiz dd = new ThamQuanBiz();
                dd.MaDiaDiem = reader["MaDiaDiem"].ToString();
                dd.ThuTu = int.Parse(reader["ThuTu"].ToString());
                list.Add(dd);
            }
            db.Close();
            foreach (var item in list)
            {
                item.TenDiaDiem = LayTenDiaDiemTheoMa(item.MaDiaDiem);
            }
            return list;
        } */
        /* public string LayTenDiaDiemTheoMa(string ma)
        {
            string ten = (string)db.GetValue("SELECT [Ten] FROM [DiaDiem] WHERE [MaDiaDiem]='" + ma + "'");
            return ten;
        } */
        public Boolean Insert(DiaDiemBiz dd)
        {
            int rs = db.ExecuteNonQuery("INSERT INTO dbo.[DiaDiem]([Ten]) VALUES (N'" + dd.Ten + "')");
            db.Close();
            return rs > 0;
        }
        public Boolean Update(DiaDiemBiz dd)
        {
            string query = " UPDATE [DiaDiem]" +
                           " SET [Ten] =  N'"+dd.Ten+"'" +
                           " WHERE [MaDiaDiem] = "+dd.MaDiaDiem;
            int rs = db.ExecuteNonQuery(query);
            return rs > 0;
        }
        public Boolean Delete(DiaDiemBiz dd)
        {
            string query = " DELETE [DiaDiem]" +
                           " WHERE [MaDiaDiem] = " + dd.MaDiaDiem;
            int rs = db.ExecuteNonQuery(query);
            return rs > 0;
        }
        public List<DiaDiemBiz> GetAllDiaDiem()
        {
            List<DiaDiemBiz> lst = new List<DiaDiemBiz>();
            string query = "SELECT * FROM [DiaDiem]";
            DataTable tbl = db.GetDateTable(query);

            foreach (DataRow r in tbl.Rows)
            {
                DiaDiemBiz dd = new DiaDiemBiz();
                dd.MaDiaDiem = (int)r["MaDiaDiem"];
                dd.Ten = (string)r["Ten"];

                lst.Add(dd);
            }

            return lst;
        }
    }
}
